<?php

class AddressController extends Zend_Controller_Action
{

    private $_siteInfo = null;

    public function init()
    {
		$this->_siteInfo = new Zend_Session_Namespace('siteid');
    }

    public function indexAction()
    {
        $req = $this->getRequest();
		$rest = new Application_Model_Rest();
		if($req->isGet()){
			$tig = $req->get('tig');
			if(!empty($tig)&&$tig =="22"){
				$districtId= $req->get('districtId');
				$zitiList = $rest->getDistrictSupplierList($districtId);
				$this->view->zitiList = $zitiList;
				$this->view->tig = 'ziti';
			}else if(!empty($tig)&&$tig =="search"){
				$this->view->key = $req->get('k');
				$this->view->result = '';
				if (!empty($this->view->key)) {
					$siteList = $rest->getSiteList(25,$this->view->key,0,0);
					$this->view->result = $siteList;
				}
			}
				
			
		}else if($req->isXMLHttpRequest()){//ajax
			$tag = $req->getPost('tag');
			if(!empty($tag) && $tag=='domainClick'){
				$domainId = $req->getPost('domainId');
				$domainName = $req->getPost('domainName');
				$domainAddress  = $req->getPost('domainAddress');
				
				$this->_siteInfo->siteid = $domainId;
				$this->_siteInfo->domainName = $domainName;
				$this->_siteInfo->domainAddress = $domainAddress;
				$this->_siteInfo->distributerId = '';
				$this->_siteInfo->distributerName = '';
				$this->_siteInfo->distributerAddress = '';
				if(!empty($this->_siteInfo->siteid)){
					echo json_encode(array('msg'=>'sessionSuccess'));
					exit;
				}else{
					echo json_encode(array('msg'=>'toAddress'));
					exit;
				}
				
			}else{
				$lat = $req->getPost('lat');
				$lon = $req->getPost('lon');
				$result = $rest->getSiteList('','',$lat,$lon);
				if(!empty($result)){
					echo json_encode(array('msg'=>$result));
					exit;
				}else{
					echo json_encode(array('msg'=>'noData'));
					exit;
				}
			}
		}
    }

    public function presaleAction()
    {
        $req = $this->getRequest();
		$auth = new Application_Model_Auth();
		if(!$auth->isLoggedIn()){
			$rurl = '/address/presale';
			$this->_redirect('/login?rurl='.$rurl);
		}
		if($req->isGet()){
			$oid = $req->get('oid');
			$this->view->oid = $oid;
			$rest = new Application_Model_Rest();
			$userid = $auth->getUserId();
			$channelId = '1003';
			$addressList = $rest->getUserPresaleAddressList($userid,$channelId);
			$this->view->addressList = $addressList;
			$this->view->sessionAddress = $this->_siteInfo->addressId;
		}
		if($req->isPost() && $req->isXMLHttpRequest()){
			$addressId = $req->getPost('addressId');
			$contact = $req->getPost('contact');
			$tel = $req->getPost('tel');
			$address = $req->getPost('address');
			
			$this->_siteInfo->orderContact = $contact;
			$this->_siteInfo->tel = $tel;
			$this->_siteInfo->address = $address;
			$this->_siteInfo->addressId = $addressId;
			if(!empty($this->_siteInfo->orderContact)&&!empty($this->_siteInfo->tel)
				&&!empty($this->_siteInfo->address)&&!empty($this->_siteInfo->addressId)){
				echo json_encode(array('msg'=>'success'));
				exit;
			}
			echo json_encode(array('msg'=>'fail'));
			exit;
		}
		
    }

    public function editpresaleAction()
    {
		$req = $this->getRequest();
		$rest = new Application_Model_Rest();
		$auth = new Application_Model_Auth();
		if(!$auth->isLoggedIn()){
			$rurl = '/address/editpresale';
			$this->_redirect('/login?rurl='.$rurl);
		}
		if($req->isGet()){
			$provinces = $rest->getAllProvince();
			
			//var_dump($citys);
			$this->view->provinces = $provinces;
			$oid = $req->get('oid');
			if(empty($oid)){
				$this->_redirect('/address/presale');
			}
			$this->view->oid = $oid;
		}
		
		if($req->isPost()){
			$contact = $req->getPost('text1');
			$tel     = $req->getPost('text2');
			$procity = $req->getPost('text3');
			$address = $req->getPost('text4');
			$code    = $req->getPost('text5');
			$oid = $req->get('oid');
			$uid = $auth->getUserId();
			if(!empty($contact) && !empty($tel) && !empty($procity) && !empty($address)){
				$data = array(
					'cityId'=>25,
					'isDefaultAds'=>0,
					'siteId'=>0,
					'tel'=>$tel,
					'contacts'=>$contact,
					'address' =>$procity.$address,
					'channelId'=>1003
				);
				$res = $rest->postUserPresaleAddress($data,$uid);
				$addressId = $res;
				if(!empty($addressId)){
					$this->_siteInfo->orderContact = $contact;
					$this->_siteInfo->tel = $tel;
					$this->_siteInfo->address = $procity.$address;
					$this->_siteInfo->addressId = $addressId;
					if(!empty($this->_siteInfo->orderContact)&&!empty($this->_siteInfo->tel)
						&&!empty($this->_siteInfo->address)&&!empty($this->_siteInfo->addressId)){
						$this->_redirect('/order/complete?type=advance&oid='.$oid);
					}
				}
			}
			$this->_redirect('/address/presale?oid='.$oid);
		}
    }

	public function cityinfoAction() {
		$req = $this->getRequest();
		if ($req->isXMLHttpRequest()) {
			$rest = new Application_Model_Rest();
			$provinceId = $req->get('provinceId');
			$citys = $rest->getCityByProvinceId($provinceId);
			echo json_encode(array('msg'=>'success','list'=>$citys));
			exit;
		}	
	}
	
	public function districtinfoAction() {
		$req = $this->getRequest();
		if ($req->isXMLHttpRequest()) {
			$cityId = $req->get('cityId');
			$rest = new Application_Model_Rest();
			$districts = $rest->getAreaByCityId($cityId);
			echo json_encode(array('msg'=>'success','list'=>$districts));
			exit;
		}	
	}

}





